package xio.ccf_201509;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

/**
 * 201509-3	模板生成系统  90分
 * @author Xio
 *
 */
public class TemplateGenerate {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int m = scanner.nextInt();
		 scanner.nextLine();
		StringBuilder sb = new StringBuilder();
		HashSet<String> set = new HashSet<>();
		
		for (int i = 0; i < n; i++) {
			String src = scanner.nextLine();
			sb.append(src).append("\n");
		}
	
		
		String data = sb.toString();
		int i = 0;
		while(i < data.length()) {
			int index =	data.indexOf("{{",i);
			if (index == -1) {
				break;
			}
			i = index;
			int indexEnd =	data.indexOf("}}",i);
			if (indexEnd == -1) {
				break;
			}
			i = indexEnd+2;
			String var = data.substring(index+2, indexEnd);
//			System.out.println("var:"+var);
			set.add(var);
		}
		
		HashMap<String, String> map = new HashMap<>();
		
		for (int j = 0; j < m; j++) {
			String str = scanner.nextLine();
			int split = str.indexOf(" ");
			if (split == -1) {
				continue;
			}
			String key = str.substring(0, split);
			String value = str.substring(split+2,str.length()-1);
//			System.out.println("key:"+key+" value:"+value);
			map.put(key, value);
		}
//		System.out.println(data);
		String newValue = "";
		for (String var : set) {
			newValue = map.get(var.trim());
			if (newValue == null) {
				newValue = "";
			}
			data = data.replaceAll("\\{\\{"+var+ "\\}\\}",newValue);
		}
		
		System.out.println(data);

		
	}
	

}
